<?php
declare (strict_types = 1);
namespace app\user\logic\teacher;

use app\common\exception\LogicException;
use app\common\logic\category\Category;
use app\course\logic\base\Course;
use app\user\model\teacher\Teacher as TeacherModel;

class TeacherCourse extends TeacherModel
{

    // 获取讲师的授课 - 讲师管理
    public function getTeacherCourse($map = [])
    {
        $field = 'id,title,course_type,cover,status,update_status,course_live_type,limit_buy_count'; //,price,sale_count

        $course = app(Course::class)
            ->field($field)
            ->with(['cover_url'])
            ->where($map)   
            ->paginate($this->getListRows())
            ->append(['course_type_text', 'user_price'])
            ->each( function($item) {
                $item->course_live_type = ($item->course_live_type == 2 && $item->limit_buy_count == 1) ? 3 : $item->course_live_type;
            });
        
        return $course;
    }

    public function addCourse($param)
    {
        // 讲师权限
        $mid     = static::$mid;
        $teacher = app(Teacher::class)->where('user_id', $mid)->findOrfail();
        // 验证数据
        $this->getValidate()->scene('add')->failException()->check($param);
        // 参数
        $param = app(Category::class)->parseFullCateParam($param);
        if ($teacher->mhm_id != 1) {
            $param['mhm_fullcategorypath'] = $param['category'];
            $param['mhm_category']         = app(Category::class)->cateImplode($param['category']);
            unset($param['category'], $param['fullcategorypath']);
        }
        if ( 2 != $param['course_type'] ) {
            $param['course_live_type'] = $param['limit_buy_count'] = 0;
        } else if ( $param['course_live_type']??false && $param['course_live_type'] == 2 ) {
            $limit_buy_count = $param['limit_buy_count']??0;
            $param['limit_buy_count'] = $limit_buy_count <= 16 ? $limit_buy_count : 16; 
        }

        $param['status']     = 1;
        $param['user_id']    = $mid;
        $param['teacher_id'] = $teacher->id;
        $param['mhm_id']     = $teacher->mhm_id;
        $param['solo']       = 1;
        // halt($param);
        // 上传
        $data = app(Course::class)::create($param);

        if ($data->isEmpty()) throw new LogicException("上传失败");

        return $data;
    }

    /**
     * 讲师编辑课程
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-20
     * @param  [type] $id    [description]
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function editCourse($id, $param)
    {
        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($param);
        // 参数
        $param = app(Category::class)->parseFullCateParam($param);

        if ($param['mhm_id'] != 1) {
            $param['mhm_fullcategorypath'] = $param['category'];
            $param['mhm_category']         = app(Category::class)->cateImplode($param['category']);
            unset($param['category'], $param['fullcategorypath']);
        }
        if ( 2 != $param['course_type'] ) {
            $param['course_live_type'] = $param['limit_buy_count'] = 0;
        } else if ( $param['course_live_type']??false && $param['course_live_type'] == 2 ) {
            $limit_buy_count          = $param['limit_buy_count']??0;
            $param['limit_buy_count'] = $limit_buy_count <= 16 ? $limit_buy_count : 16;
        }
        
        // 更新
        $data = Course::update($param, ['id' => $id]);

        // 返回
        return $data;
    }

	/**
     * 完结课时
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-21
     * @param  int    $id [description]
     * @return [type]     [description]
     */
    public function finishCourse(int $id): void
    {
        $course = app(Course::class)->findOrFail($id);
        $course->update_status = 0;
        if ( !$course->save() ) throw new LogicException("完结失败");
        
    }
}
